package com.compass.doctor.ui.prescription.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.DialogPrescriptionRecordItemBinding
import com.compass.doctor.databinding.LayoutUsefulExpressionsItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.onClick
import com.compass.framework.ext.saveAs
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.Drug
import com.compass.framework.model.TemplateDetails
import com.compass.framework.utils.BigDecimalUtils

/**
 * Description： dialog开药记录adapter
 * Date:2023/12/15 21:18
 */
class PrescriptionRecordAdapter  constructor(private var itemType: Int) :
    BaseMultiItemAdapter<TemplateDetails>() {

    private lateinit var onChatBottomClick: OnCustomButtonClick

    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onChatBottomClick = onChatBottomClick
    }
    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: TemplateDetails?,
        position: Int
    ) {
        if (item == null) return
        //  1、开药记录弹窗 item 2待定
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<DialogPrescriptionRecordItemBinding>()
                binding.tvDiagnoseContent.text = getDiagnosis(item.diagnosis)
                binding.tvDrugContent.text = getDrugs(item.drugs)
                binding.tvInquiryDuration.text = "￥${BigDecimalUtils.divide(item.price)}"

                binding.tvExamine.onClick {
                    onChatBottomClick.onButtonCallBack(position,"examine")
                }
                binding.tvUseTemplate.onClick {
                    onChatBottomClick.onButtonCallBack(position,"use")
                }
            }

            else -> {
                val binding = holder.binding.saveAs<LayoutUsefulExpressionsItemBinding>()
                binding.actionText.text = item.name
            }
        }


    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                DialogPrescriptionRecordItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutUsefulExpressionsItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }

    private fun getDiagnosis(list:MutableList<Diagnosis>?):String{
        list?.let {
            for (item in list){
                if (item.main) return item.name
            }
        }
        return ""
    }

    private fun getDrugs(list:MutableList<Drug>?):String{

        var listName=ArrayList<String?>()
        list?.let {
            for (item in list){
                listName.add("${item.name}x${item.num}")
            }
        }
        return  listName.joinToString(separator ="/")
    }
}